package cn.iocoder.yudao.module.system.controller.admin.customerservice;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.customerservice.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.customerservice.CustomerServiceDO;
import cn.iocoder.yudao.module.system.service.customerservice.CustomerServiceService;

@Tag(name = "管理后台 - 客户服务")
@RestController
@RequestMapping("/system/customer-service")
@Validated
public class CustomerServiceController {

    @Resource
    private CustomerServiceService customerServiceService;

    @PostMapping("/create")
    @Operation(summary = "创建客户服务")
    @PreAuthorize("@ss.hasPermission('system:customer-service:create')")
    public CommonResult<Integer> createCustomerService(@Valid @RequestBody CustomerServiceSaveReqVO createReqVO) {
        return success(customerServiceService.createCustomerService(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户服务")
    @PreAuthorize("@ss.hasPermission('system:customer-service:update')")
    public CommonResult<Boolean> updateCustomerService(@Valid @RequestBody CustomerServiceSaveReqVO updateReqVO) {
        customerServiceService.updateCustomerService(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户服务")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:customer-service:delete')")
    public CommonResult<Boolean> deleteCustomerService(@RequestParam("id") Integer id) {
        customerServiceService.deleteCustomerService(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户服务")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:customer-service:query')")
    public CommonResult<CustomerServiceRespVO> getCustomerService(@RequestParam("id") Integer id) {
        CustomerServiceDO customerService = customerServiceService.getCustomerService(id);
        return success(BeanUtils.toBean(customerService, CustomerServiceRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户服务分页")
    @PreAuthorize("@ss.hasPermission('system:customer-service:query')")
    public CommonResult<PageResult<CustomerServiceRespVO>> getCustomerServicePage(@Valid CustomerServicePageReqVO pageReqVO) {
        PageResult<CustomerServiceDO> pageResult = customerServiceService.getCustomerServicePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerServiceRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户服务 Excel")
    @PreAuthorize("@ss.hasPermission('system:customer-service:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomerServiceExcel(@Valid CustomerServicePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerServiceDO> list = customerServiceService.getCustomerServicePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户服务.xls", "数据", CustomerServiceRespVO.class,
                        BeanUtils.toBean(list, CustomerServiceRespVO.class));
    }

}